`mapCountryData` <-function(dataset,fieldToPlot,...){data(dFexampleCountryData,envir=environment(),package="rworldmap");mapCountryData(get("dFexampleCountryData"),"BIODIVERSITY")}
setGeneric("mapCountryData")

setMethod("mapCountryData",c("SpatialPolygonsDataFrame","character"),
function(               dataset
                        ,fieldToPlot
                        ,projection="none" #, "EqualArea"
                        ,numCats = 7 # *may be overridden by catMethod
                        ,we=-160
                        ,ea=160
                        ,so=-80
                        ,no=90
                        ,mapRegion = "world"   #sets map extents, overrides we,ea etc.
                        ,catMethod="pretty"   #any vector defining breaks, "fixedWidth","quantiles","logFixedWidth"
                        ,colourPalette= "heat" #"heat","white2Black","topo","palette" for current palette
                        ,addLegend=TRUE
                        ,countryBorderCol = 'grey'
                        ,mapTitle = 'columnName' #this sets it to the name of the column, any other string can be passed too
                        ,oceanColour=NA
                        ,aspect=NA
                        ,missingCountryColour=NA
                        ,...){
  require(sp)
  mapToPlot<-dataset
  nameColumnToPlot<-fieldToPlot


   ## depending on the catMethod, the numCats may be overriden (e.g. for 'pretty')
  dataCategorised <- rwmApplyClassBreaks( mapToPlot@data[[nameColumnToPlot]], catMethod, numCats)

  ## add extra column to map attribute data
  colNameRaw <- nameColumnToPlot
  colNameCat <- paste(colNameRaw,"categorised",sep='')
  mapToPlot@data[[colNameCat]] <- dataCategorised

  ## how many colours : numCats may be overriden (e.g. for 'pretty')
  numColours <- length(levels(dataCategorised))

  ## get vector of the colours to be used in map (length=num categories)
  coloursForMap <- rwmGetColours(colourPalette,numColours)

  ## get numeric index of which category each datapoint is in (length = num points)
  dataCatNums <- as.numeric(dataCategorised)


  #adding missing country colour
  if(!is.na(missingCountryColour)){
  coloursForMap<- c(coloursForMap,missingCountryColour)
  dataCatNums[is.na(dataCatNums)]<-length(coloursForMap)
  }


  #Basic map plotting
  newMap(mapToPlot,projection=projection,mapRegion=mapRegion,oceanColour=oceanColour,aspect=aspect)
  plot(mapToPlot,col=coloursForMap[dataCatNums],border=countryBorderCol,add=TRUE)

  ## adding a simple legend
  ##! later offer option to modify this
  if (addLegend){
    if(require("spam") && require("fields")){
      addMapLegend(mapToPlot@data[[nameColumnToPlot]],catMethod=catMethod,colourPalette=colourPalette,numCats=numCats)
    }else{
      ## Old style legend if you don't have spam or fields.
      legend(x='bottomleft', legend=c(rev(levels(dataCategorised)),"no data"), pch = 15, col=c(coloursForMap[numColours:1],"white"), title="category",bg="white" )
    }
  }

  ## add title
  if ( mapTitle == 'columnName' ){
    title(nameColumnToPlot)
  } else {
    title( mapTitle )
  }



}
)


setMethod("mapCountryData",c("data.frame","character"),
function(dataset,fieldToPlot,joinCode="ISO3",nameCountryColumn="Country",nameJoinColumn="ISO3V10", suggestForFailedCodes=FALSE, projection="none", mapResolution="low", verbose=TRUE,...){
mapToPlot <- joinCountryData2Map(dF=dataset, joinCode=joinCode, nameJoinColumn=nameJoinColumn, nameCountryColumn=nameCountryColumn, suggestForFailedCodes=suggestForFailedCodes, projection=projection, mapResolution=mapResolution, verbose=verbose)
mapCountryData(mapToPlot,fieldToPlot,...)
}
)

setMethod("mapCountryData",c("data.frame","formula"),
function(dataset,fieldToPlot,...){
newdata<-model.frame(fieldToPlot,dataset)
newDataSet<-cbind(dataset,newdata)
mapCountryData(newDataSet,names(newDataSet)[ncol(newDataSet)],...)
}
)
